<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tally_category".
 *
 * @property int $id 分类ID
 * @property string $title 分类名称
 * @property int $tally_id 账本ID
 * @property int $uid 创建者
 * @property int $type 类型匹配账套
 * @property string $pic 账套图片
 * @property int $pid 上级类别
 * @property int $is_sys 是否为默认
 * @property int $status 状态0删除1正常
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 */
class TallyCategory extends \yii\db\ActiveRecord
{

    /**
     * 有效状态
     */
    const STATUS_VALID = 1 ;

    /**
     * 无效状态
     */
    const STATUS_INVALID = 0 ;

    // 挂账大分类ID
    const GZ_PID = '90';

    // 应收款分类ID
    const GZ_YS_ID = '98';

    // 应付款分类ID
    const GZ_YF_ID = '99';

    // 挂账操作涉及ID
    public static $gzIds = [self::GZ_YS_ID,self::GZ_YF_ID];

    // 应收款类别追加
    public static $ysExt =  [
        'id' => self::GZ_PID,
        'title'=> '挂账',
        'pid'=> '0',
        'type'=> '1',
        'pic'=> '',
        'is_sys'=> '1',
        'children' => [
            [
                'id' => self::GZ_YS_ID,
                'title'=> '应收款',
                'pid'=> '0',
                'type'=> '1',
                'pic'=> '',
                'is_sys'=> '1',
            ]
        ]
    ];

    // 应付款类别追加
    public static $yfExt =  [
        'id' => self::GZ_PID,
        'title'=> '挂账',
        'pid'=> '0',
        'type'=> '2',
        'pic'=> '',
        'is_sys'=> '1',
        'children' => [
            [
                'id' => self::GZ_YF_ID,
                'title'=> '应付款',
                'pid'=> '0',
                'type'=> '2',
                'pic'=> '',
                'is_sys'=> '1',
            ]
        ]
    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tally_category';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'tally_id','type', 'pid', 'is_sys', 'status'], 'integer'],
            [['uid', 'tally_id','type', 'title'], 'required'],
            [['created_time', 'updated_time'], 'safe'],
            [['title'], 'string', 'max' => 600],
            [['pic'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'tally_id' => 'Tally Id',
            'uid' => 'Uid',
            'type' => 'Type',
            'pic' => 'Pic',
            'pid' => 'Pid',
            'is_sys' => 'Is Sys',
            'status' => 'Status',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
        ];
    }

    /**
     * 根据挂账类别返回类别名称
     * @param $cateId
     *
     * @return string
     */
    public static function getGzCateName($cateId) {
        if($cateId == self::GZ_YS_ID ) {
            return '应收款';
        }else if($cateId == self::GZ_YF_ID ) {
            return '应付款';
        }else if( $cateId == self::GZ_PID ) {
            return '应收应付';
        }else {
            return '';
        }
    }
}
